﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace TitipNyobaYan.Controllers
{
    class Control <T>
    {
        public List<T> view()
        {
            try
            {
                using (var session = Controllers.EntitiesController.OpenSession())
                {
                    return session.Query<T>().ToList();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return null;
        }
        public void tambah(T input)
        {
            using (var session = Controllers.EntitiesController.OpenSession())
            {
                using (var transaksi = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(input);
                        transaksi.Commit();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
                
            }
        }
        public void hapus(string input)
        {
            using (var session = Controllers.EntitiesController.OpenSession())
            {
                using (var transaksi = session.BeginTransaction())
                {
                    var temp = session.Load<T>(input);
                    session.Delete(temp);
                    transaksi.Commit();
                }
            }
        }
        public void edit(T input)
        {
            try
            {
                using (var session = Controllers.EntitiesController.OpenSession())
                {
                    using (var transaksi = session.BeginTransaction())
                    {
                        session.Update(input);
                        transaksi.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        public T get(string input)
        {
            using (var session = Controllers.EntitiesController.OpenSession())
            {
                using (var transaksi = session.BeginTransaction())
                {
                    var temp = session.Get<T>(input);
                    return temp;
                }
            }
        }
    }
}
